package com.qinhai.webcs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qinhai.webcs.pojo.Cart;
import com.qinhai.webcs.pojo.Product;
import com.qinhai.webcs.pojo.User;
import com.qinhai.webcs.service.CartService;
import com.qinhai.webcs.service.ProductService;
import com.qinhai.webcs.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 秦海
 * @since 2021-04-11
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private ProductService productService;

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(
            @PathVariable("productId") Integer productId,
            @PathVariable("price") float price,
            @PathVariable("quantity") Integer quantity,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", productId);
        Cart cartOne = cartService.getOne(queryWrapper);

        if (cartOne != null) {
            cartOne.setQuantity(cartOne.getQuantity() + quantity);
            cartOne.setCost(cartOne.getCost() + (price * quantity));
            cartService.updateById(cartOne);
        } else {
            Cart cart = new Cart();
            cart.setProductId(productId);
            cart.setQuantity(quantity);
            cart.setCost(quantity * price);
            cart.setUserId(user.getId());
            cartService.save(cart);
        }

        //库存减少
        Product product = productService.getById(productId);
        int stock = product.getStock() - quantity;
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", productId);
        updateWrapper.set("stock", stock);
        productService.update(updateWrapper);

        return "redirect:/cart/findAllCart";
    }

    @GetMapping("/findAllCart")
    public ModelAndView findAllCart(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement1");
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    //删除一个购物车信息
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id) {


        //将库存添加回去
        Cart cart = cartService.getById(id);
        Product product = productService.getById(cart.getProductId());
        product.setStock(product.getStock() + cart.getQuantity());
        productService.updateById(product);
        cartService.removeById(id);

        return "redirect:/cart/findAllCart";
    }


    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user.getId());
        modelAndView.addObject("addressList", userAddressService.list(queryWrapper));
        return modelAndView;
    }

    @PostMapping("/update/{id}/{quantity}/{cost}")
    @ResponseBody
    public String updateCart(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Float cost
    ) {
        Cart cart = cartService.getById(id);
        cart.setQuantity(quantity);
        cart.setCost(cost);

        boolean result = cartService.updateById(cart);

        if (result) {
            return "success";
        } else {
            return "fail";
        }
    }
}

